package com.example.hp.qingyi.compass.fragment.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;


import com.example.hp.qingyi.R;
import com.example.hp.qingyi.base.BaseFragment;
import com.example.hp.qingyi.compass.fragment.fragment.fragment.AusleseFragment;
import com.example.hp.qingyi.compass.fragment.fragment.fragment.HotspotFragment;
import com.example.hp.qingyi.compass.fragment.fragment.fragment.SquareFragment;

import java.util.ArrayList;

/**
 * 发现页面
 */
public class CompassFragment extends BaseFragment {

    private TabLayout tabLayout;
    private ArrayList<Fragment> arrayList;
    private  String[] mTitles = new String[]{"推荐热点", "驿友精选", "问答广场"};
    private FragmentManager fragmentManager;
    private ViewPager viewPager;

    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.compass_view,null);

        tabLayout = view.findViewById(R.id.tab_find_hotspot);
        arrayList = new ArrayList<>();
        viewPager = view.findViewById(R.id.vp_find);
        fragmentManager = getActivity().getSupportFragmentManager();

        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.addTab(tabLayout.newTab().setText(mTitles[0]));
        tabLayout.addTab(tabLayout.newTab().setText(mTitles[1]));
        tabLayout.addTab(tabLayout.newTab().setText(mTitles[2]));

        arrayList.add(new HotspotFragment());
        arrayList.add(new AusleseFragment());
        arrayList.add(new SquareFragment());

//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(fragmentManager);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Log.i("initData","发现");
    }


    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            return mTitles.length;
        }

        //ViewPager与TabLayout绑定后，这里获取到PageTitle就是Tab的Text
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }
}
